package com.iscale.controller;

import com.iscale.model.entity.SysBusinessType;
import com.iscale.model.entity.SysDictionary;
import com.iscale.service.SysBusinessTypeService;
import com.iscale.service.SysDictionaryService;
import com.iscale.util.ResponseCode;
import com.iscale.util.Result;
import com.iscale.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.iscale.controller.BaseController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 业务类型 前端控制器
 * </p>
 *
 * @author Andychen
 * @since 2017-03-10
 */
@Api(value = "业务类型管理模块",description = "业务类型管理模块")
@Controller
@RequestMapping("/sysBusinessType")
public class SysBusinessTypeController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(SysBusinessTypeController.class);

    @Autowired
    SysBusinessTypeService businessTypeService;

    @Autowired
    ThreadPoolTaskExecutor threadPoolTaskExecutor;


    @ApiOperation(value = "添加/更业务类型", httpMethod = "POST", produces = "application/json", response = Result.class)
    //    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "saveOrUpdateBusiness", method = RequestMethod.POST)
    public Result saveOrUpdateBusiness(
            @ApiParam(value = "名称", required = true) @RequestParam(defaultValue = "", required = true) String name,
            @ApiParam(value = "描述", required = false) @RequestParam(defaultValue = "", required = false) String description,
            @ApiParam(value = "状态", required = false) @RequestParam(defaultValue = "", required = false) Integer status,
            @ApiParam(value = "是否可删除", required = false) @RequestParam(defaultValue = "", required = false) Integer isFinal
    ) throws Exception {
        //String serviceCode, String name, String description, String createUser, Integer isFinal,Integer status
        SysBusinessType sysBusinessType = new SysBusinessType("",name,description,"",isFinal,status);
        boolean result = businessTypeService.insertOrUpdate(sysBusinessType);
        if(result){
            if(StringUtil.isEmpty(sysBusinessType.getServiceCode())){
                sysBusinessType.setServiceCode(String.valueOf(sysBusinessType.getId()));
                businessTypeService.updateById(sysBusinessType);
            }
            return Result.success(sysBusinessType);
        }else{
            return Result.error(ResponseCode.add_business_error);
        }
    }

    @ApiOperation(value = "删除务类型", httpMethod = "POST", produces = "application/json", response = Result.class)
    //    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "deleteBusiness", method = RequestMethod.POST)
    public Result deleteBusiness(
            @ApiParam(value = "ID", required = true) @RequestParam(defaultValue = "", required = true) String id
    ) throws Exception {
        boolean result = businessTypeService.deleteById(id);
        if(result){
            return Result.success("");
        }else{
            return Result.error(ResponseCode.add_business_error);
        }
    }

    @ApiOperation(value = "查询业务类型", httpMethod = "POST", produces = "application/json", response = Result.class)
    //    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "selectBusiness", method = RequestMethod.POST)
    public Result selectBusiness(
            @ApiParam(value = "ID", required = false) @RequestParam(defaultValue = "", required = false) Long id,
            @ApiParam(value = "名称", required = false) @RequestParam(defaultValue = "", required = false) String name
    )   {
        List<SysBusinessType> sysBusinessTypeList = businessTypeService.selectBusinessTypes(id,name);
        return Result.success(sysBusinessTypeList);
    }
}
